package com.geniusdog.monotlights;

import java.io.DataOutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View;
import android.widget.Toast;
import android.os.SystemClock;
import java.util.Calendar;
import com.geniusdog.monotlights.R;


@SuppressWarnings("unused")
public class NotificationLights extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // not sure, but i had to do it for it to work :\
        RelativeLayout container = new RelativeLayout(this);
        
        // define strings for button text (could be done better but I'm incompetent)
        String startstring = new String();
        String stopstring = new String();
        String errstring = new String();
        startstring = "Light Buttons";
        stopstring = "Stop Buttons";
        errstring = "Couldn't get SU, are you rooted?";
        
        // define buttons, simple enough
        Button startbutton = new Button(this);
        Button stopbutton = new Button(this);
        startbutton.setText(startstring);
        startbutton.setOnClickListener(clickstart);
        stopbutton.setText(stopstring);
        stopbutton.setOnClickListener(clickstop);
        
        // define button positions
        RelativeLayout.LayoutParams startparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        startparam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        RelativeLayout.LayoutParams stopparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        stopparam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        
        //add buttons to the view and then set the view
        container.addView(startbutton, startparam);
        container.addView(stopbutton, stopparam);
        setContentView(container);
    }
  
 
    // Set up the listener for the start button click event
	private OnClickListener clickstart = new OnClickListener() {
	    public void onClick(View v) {
	    	// define needed stuff
	        Process process = null;
	        DataOutputStream os = null;
	            try {
	            // get root	
	            process = Runtime.getRuntime().exec("su");
	            os = new DataOutputStream(process.getOutputStream());
	            // write the command
	            os.writeBytes("echo 100 > /sys/class/leds/button-backlight/brightness\n");
	            os.writeBytes("exit\n");
	            // clear the buffer
	            os.flush();
	            Toast.makeText(NotificationLights.this, "Lights are on", Toast.LENGTH_SHORT).show();
	            // wait for complete
	            process.waitFor();
	            
	            // won't catch an error with root, but it has to have an exception catcher to execute
	            } catch (Exception e) {
	            	Toast.makeText(NotificationLights.this, "Couldn't get SU, are you rooted?", Toast.LENGTH_SHORT).show();
	                return;
	            }
	    }
	};
	
	private OnClickListener clickstop = new OnClickListener() {
	    public void onClick(View v) {
	      // all same as above, just to stop lights instead
	        Process process = null;
	        DataOutputStream os = null;
	            try {
	            // even though should have root already, get it again just in case ;)
	            process = Runtime.getRuntime().exec("su");
	            os = new DataOutputStream(process.getOutputStream());
	            os.writeBytes("echo 0 > /sys/class/leds/button-backlight/brightness\n");
	            os.writeBytes("exit\n");
	            os.flush();
	            process.waitFor();
	            
	            } catch (Exception e) {
	            	Toast.makeText(NotificationLights.this, "Couldn't get SU, are you rooted?", Toast.LENGTH_SHORT).show();
	                return;
	            }
	    }
	};


	
}
